package com.emar.spring.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletRequest;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.emar.spring.dto.ChartDTO;
import com.emar.spring.dto.ParamsDTO;
import com.emar.spring.dto.SingleDataDTO;
import com.emar.spring.model.ProjectEntity;
import com.emar.spring.service.ProjectService;

/**
 * 操作项目表的控制器类
 */
@Controller
@RequestMapping(value="/project")
public class ProjectController {
	@Autowired
	private ProjectService projectService;

	/**
	 * 获取项目列表
	 * @param pe	封装好的项目查询条件
	 * @param page	
	 * @param rows	
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/getprojectlist",method=RequestMethod.POST)
	@ResponseBody
	public String getProjectList(@ModelAttribute ProjectEntity pe,
			@RequestParam(value="page",defaultValue="0") int page,
			@RequestParam(value="rows",defaultValue="10") int rows,
			Model model){
		
		int offset = (page-1)*rows;
		int total = projectService.getProjectCount(pe, page, rows);
		List<ProjectEntity> proList = projectService.getProjectList(pe,offset, rows); 
		
		JSONArray arr = new JSONArray(proList);
		JSONObject data = new JSONObject();
		data.put( "total", total);
		data.put( "rows", arr);
		
		return data.toString();
	}
	
	/**
	 * 获取项目查询结果，并封装为json格式，传回前台的fushionchart，以饼状图展示
	 * @param groupBy
	 * @param totalNumber
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/getpie",method=RequestMethod.POST)
	public String ownerGetByDayPie(
			@RequestParam(defaultValue="commissionCorpName") String groupBy,
			@RequestParam(defaultValue="100") int totalNumber,
			Model model,ServletRequest request){
		ChartDTO chart = new ChartDTO( "完成进度", "", "",  " (s)",  "", "0");
		JSONObject json = new JSONObject();
		JSONObject chart1 = new JSONObject(chart);
		json.put( "chart",  chart1);
		List<ParamsDTO> list = projectService.getProjectDTOList(new ProjectEntity(), groupBy, 0, 100);
		int totalOk = projectService.getCorpCount(groupBy);	//已完成总量，以公司名称分组下的。
		
		List<SingleDataDTO> singleList = new ArrayList<SingleDataDTO>();
		if("commissionCorpName".equals(groupBy)){
			for(ParamsDTO r :list){
				SingleDataDTO s = new SingleDataDTO();
				s.setLabel( r.getCommissionCorpName());
				s.setValue( String.valueOf(r.getCount()));
				singleList.add(s);
			}
		}else if("projectType".equals(groupBy)){
			for(ParamsDTO r :list){
				SingleDataDTO s = new SingleDataDTO();
				s.setLabel( r.getProjectType());
				s.setValue( String.valueOf(r.getCount()));
				singleList.add(s);
			}
		}
		SingleDataDTO s = new SingleDataDTO();
		s.setLabel( "未完成量");
		s.setValue( String.valueOf(totalNumber-totalOk));
		singleList.add(s);
		
		json.put( "data", singleList);
		
		model.addAttribute( "json", json);
		model.addAttribute( "url", "adminpie");
		return "chartdemo/pie";
	}
}
